package com.zhong.gang.applib.ui.chese;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.tools.ToastUtils;
import com.zhong.gang.applib.CheseListener;
import com.zhong.gang.applib.R;
import com.zhong.gang.applib.base.act.ActBaseRefresh;
import com.zhong.gang.applib.config.Config;
import com.zhong.gang.applib.databinding.LayoutPullToBinding;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.HttpUtils;

import java.util.ArrayList;
import java.util.List;

public class MaterialAct extends ActBaseRefresh<LayoutPullToBinding, MaterialAdapter> implements BaseQuickAdapter.OnItemClickListener {
    private static CheseListener cheseListener;
    public static final void openAct(Context context
            , String url, String materialYardId
            , String type,boolean isLogin,String token,String userID,String roleId, CheseListener cheseListener){
        Intent intent = new Intent(context, MaterialAct.class);
        intent.putExtra(Config.URL,url);
        intent.putExtra(Config.MATERIALYARDID,materialYardId);
        intent.putExtra(Config.TYPE,type);
        intent.putExtra(Config.IS_LOGIN,isLogin);
        intent.putExtra(Config.TOKEN,token);
        intent.putExtra(Config.USER_ID,userID);
        intent.putExtra(Config.ROLE_ID,roleId);
        context.startActivity(intent);
        MaterialAct.cheseListener = cheseListener;
    }
    @Override
    public int LayoutRes() {
        return R.layout.layout_pull_to;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "选择内容";
    }

    @Override
    public void initView() {
        setAddMore(false);
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initrefrsh(bind.pullto,new MaterialAdapter(),bind.recyclerView);
        adapter.setOnItemClickListener(this);
        getData();
    }


    @Override
    public void getData() {
        super.getData();
        HttpUtils.get(getIntent().getStringExtra(Config.URL),getIntent().getBooleanExtra(Config.IS_LOGIN,false)
                ,getIntent().getStringExtra(Config.TOKEN),getIntent().getStringExtra(Config.ROLE_ID),getIntent().getStringExtra(Config.USER_ID))
                .addParams("materialYardId", getIntent().getStringExtra(Config.MATERIALYARDID))
                .addParams("type",getIntent().getStringExtra(Config.TYPE))
                .build().execute(new ActCallBack<BaseDataBean<List<MaterialTypeBean>>>(){
            @Override
            public void onResponse(BaseDataBean<List<MaterialTypeBean>> data) throws Exception {
                super.onResponse(data);
                setData(data.getResult());
            }

            @Override
            public void onError(String str) {
                super.onError(str);
                setData(new ArrayList<MaterialTypeBean>());
            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        MaterialTypeBean materialTypeBean = (MaterialTypeBean) adapter.getItem(position);
        if (materialTypeBean.getMaterialTypePrice() == null){
            ToastUtils.s(this,"料型不合法");
            return;
        }
        closeActivity();
        if (cheseListener == null)
            return;
        cheseListener.onCheseBack(materialTypeBean);
    }
}
